/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
import { Platform } from '../utils';
import mergeJSON from '../utils/mergeJSON';
// eslint-disable-next-line no-unused-vars
import { IAreaConfigItem, IConfig } from './template';
import i18n from 'i18n-js';
import config from './template_ext.json';


let NEW_CONFIG: IConfig; // 缓存计算好的配置

function getConfig(): IConfig {
  return config;
}

/**
 * 把配置文件计算一下，补充默认值
 */
const templateConfig: IConfig = getConfig();

// 应用程序相关的配置文件
const baseConfig: IConfig = {
  app: {
    name: '智能插座', // 中文名
    appleID: '', // apple 的 appid
    bundleID: 'com.gizwits.socket', // 包名
    // 应用图标 启动图 直接替换cordova工程资源文件
    // 证书保存在服务器
    color: {
      topNavBackground: '#fff',
      bottomNavActivationColor: '#fd9d38',
      topNavColor: '#000',
      bottomNavBackground: '#fff',
      bottomNavColor: '#909090',
      tintColor: '#FFB32E',
    },
    fontSize: {
      title: 30,
      deviceState: 26,
      pageTitle: 24,
      blockTitle: 18,
      text: 15,
      subText: 13,
      smallText: 12
    },
    // 暗黑模式颜色配置，如果只需要改动部分颜色配置，可不配置，会默认合并color配置
    darkColor: {
      tintColor: '#FED530',
    },
    networkConfigType: ['SOFT_AP'], // 枚举 AIR_LINK SOFT_AP AUTO
    JPush: {}, // 推送的相关key
    cloudServiceInfo: null, // 默认不指向
  },

  // 机智云相关配置
  gizwits: {
    iosAppID: 'f50095a20a2f4152a3685a01a863ec5a',
    iosAppSecret: '20438225160440b7b5a8d87ed8abcad7',
    androidAppID: 'afb8f41dbb274cb19d0fdf472945fbf3',
    androidAppSecret: '3d81fcd4c22e48a286c26a6b3bf29a5b',
  },

  // 高德地图配置
  AMap: {
    androidKey: 'assdasda', // 高德开发平台申请的appKey
  },

  // 关于我们页面
  aboutUs: {
    title: config.app.name || 'Gizwits',
    logo: null,
    copyRight: false,
    content: [],
  },
  quickLogin: [], // 第三方登录的key
  needLicense: true, // 是否需要增加协议
  voiceTips: {},
  // 是否支持第三方设备接入
  supportThirdDevices: false,
  // 是否支持统计
  supportStatistical: false,
  // 登录页面
  login: {},
  // 是否显示消息中心
  notificationCenter: {
    enable: false,
  },

  supportLanguages: [],
  supportNotice: [
    {
      id: 'SMS',
      enable: false,
    },
    {
      id: 'PHONE',
      enable: false,
    },
  ],

  language: {
    zh: {
      LOGIN_TITLE: config.app.name || 'Gizwits',
    },
  }
};

mergeJSON(baseConfig, templateConfig);

const coverLanguage = (main: any, secondary: any) => {
  let data = Object.assign({}, main);
  data = Object.assign(data, secondary);
  return data;
};

// 检查文案是否有空
const checkLanguage = (language: any) => {
  // eslint-disable-next-line no-restricted-syntax
  for (const key in language) {
    if (language[key] === '') {
      // eslint-disable-next-line no-param-reassign
      language[key] = ' ';
    }
  }
};
if (!baseConfig.language) {
  baseConfig.language = {
    zh: {},
  };
}
if (!baseConfig.language.en) {
  baseConfig.language.en = baseConfig.language.zh;
} else {
  baseConfig.language.en = coverLanguage(
    baseConfig.language.zh,
    baseConfig.language.en
  );
}
// console.log('====', baseConfig);
(baseConfig.supportLanguages || []).map((languageItem) => {
  if (!baseConfig.language[languageItem.key]) {
    baseConfig.language[languageItem.key] = baseConfig.language.zh;
  } else {
    baseConfig.language[languageItem.key] = coverLanguage(
      baseConfig.language.zh,
      baseConfig.language[languageItem.key]
    );
  }
});

for (const key in baseConfig.language) {
  checkLanguage(baseConfig.language[key]);
}
// eslint-disable-next-line prefer-const
NEW_CONFIG = baseConfig;

/**
 * 把配置文件计算一下，补充默认值
 */

/**
 * 获取配置文件里的语言
 */
export function getTemplateLanguage(id: string, key: string) {
  // 取前两位
  // eslint-disable-next-line no-param-reassign
  key = key.substring(0, 2);
  const tconfig = getConfig();
  if (tconfig.language && tconfig.language[key]) {
    return tconfig.language[key][id];
  }
  return id;
}

export function getTemplateConfig() {
  return NEW_CONFIG;
}

export function getTemplateConfUpdatedAt() {
  const tconfig = getConfig();
  return tconfig.updated_at;
}

export function getAmapKey(): string {
  const tconfig = getConfig();
  if (tconfig.AMap) {
    return tconfig.AMap.androidKey;
  }
  return '';
}

export function getLanguageConfig() {
  const tconfig = getTemplateConfig();
  return tconfig.supportLanguages;
}

export function getTheme() {
  const tconfig = getTemplateConfig();
  return { ...tconfig.app.color, fontSize: tconfig.app.fontSize };
}

export function getAppName(languageKey: string) {
  const tconfig = getTemplateConfig();
  const templateLanguage = tconfig.language[languageKey]
    || tconfig.language[languageKey.slice(0, 2)];
  return (templateLanguage && templateLanguage.APP_NAME) || tconfig.app.name;
}

export function getSceneConfig() {
  const tConfig = getTemplateConfig();
  return tConfig.sceneSupport;
}

export function getSceneConditionsConfig() {
  const sceneConfig = getSceneConfig();
  if (sceneConfig) {
    return sceneConfig.conditions;
  }
  return undefined;
}

export function getSceneWeatherConfig() {
  const sceneConditonsConfig = getSceneConditionsConfig();
  if (sceneConditonsConfig) {
    return sceneConditonsConfig.find(i => i.id === 'WEATHER');
  }
  return undefined;
}


// 是否有比普通手机号码登录等级更高的登录方式
export function highLevelThanNormalLogin() {
  const tConfig = getTemplateConfig();
  const configInfo = tConfig.supportMobileNumberQuickLogin;
  if (configInfo && configInfo.level) {
    return configInfo.level > 10;
  }
  return false;
}

export function isSuperApp() {
  const tConfig = getTemplateConfig();
  return tConfig.isSuperApp || false;
}

export function getAppInfo(languageKey: string) {
  const tConfig = getTemplateConfig();
  let icon;
  if (tConfig && tConfig.login) {
    icon = tConfig.login.logo;
  }
  const appName = getAppName(languageKey);
  return { name: appName, icon };
}


export function getAppIDInfo() {
  const gconfig = getTemplateConfig();
  return { appId: gconfig.gizwits.wxAppID, appSecret: gconfig.gizwits.wxAppSecret } as IAppBaseInfo;
}

export function getWxTabbarConfig() {
  const gconfig = getTemplateConfig();
  if (gconfig.wxApp) {
    return gconfig.wxApp.tabbar;
  }
  return undefined;
}

export function getWxCustomerServiceConfig() {
  const gconfig = getTemplateConfig();
  if (gconfig.wxApp) {
    return gconfig.wxApp.customerService;
  }
  return undefined;
}

// 按传入查询是否支持某一种通知
export function getSupportNoticeConfig(id: string) {
  const gconfig = getTemplateConfig();
  return gconfig.supportNotice && (gconfig.supportNotice.find(i => i.id == id) || {}).enable;
}

export function getPhoneNotiConfig() {
  return getSupportNoticeConfig('PHONE');
}

export function getSMSNotiConfig() {
  return getSupportNoticeConfig('SMS');
}

export function getWxNotiConfig() {
  return getSupportNoticeConfig('WECHAT');
}

export function supportNotice() {
  return getPhoneNotiConfig() || getSMSNotiConfig() || getWxNotiConfig();
}

export function supportExperienceCenter() {
  const gconfig = getTemplateConfig();
  return (gconfig.experienceCenter || {}).enable;
}

export function supportWechatPay() {
  const gconfig = getTemplateConfig();
  return ((gconfig.wallet || {}).wechat || {}).enable;
}

export function showCategory() {
  const gconfig = getTemplateConfig();
  return gconfig.showCategory;
}

export function getScanServices() {
  const gconfig = getTemplateConfig();
  return (gconfig.wxApp || {}).scanServices;
}


const defaultFirstAreaConfig: IAreaConfigItem = {
  enable: true, areaName: 'HOME', defaultName: 'MY_HOME'
}; // 默认支持第一级别的区域，家庭
const defaultSecondAreaSupport: IAreaConfigItem = {
  enable: true, areaName: 'ROOM', defaultName: 'DEFAULT'
}; // 默认支持第二级别的区域，房间

function getAreaConfig() {
  const tConfig = getTemplateConfig();
  return tConfig.areaConfig;
}

export function getFirstAreaSupport() {
  // 小程序还没支持家庭，直接返回false
  return false;
  // const areaConfig = getAreaConfig();
  // return areaConfig?.firstArea?.enable ?? defaultFirstAreaConfig.enable;
}

export function getFirstAreaName() {
  const areaConfig = getAreaConfig();
  return i18n.t(areaConfig?.firstArea?.areaName ?? defaultFirstAreaConfig.areaName);
}

export function getFirstAreaDefaultName() {
  const areaConfig = getAreaConfig();
  return i18n.t(areaConfig?.firstArea?.defaultName ?? defaultFirstAreaConfig.defaultName);
}

export function getSecondAreaSupport() {
  // 小程序还没支持房间，直接返回false
  return false;
  const areaConfig = getAreaConfig();
  console.log('getSecondAreaSupport', areaConfig?.secondArea?.enable)
  return areaConfig?.secondArea?.enable ?? defaultSecondAreaSupport.enable;
}

export function getSecondAreaName() {
  const areaConfig = getAreaConfig();
  return i18n.t(areaConfig?.secondArea?.areaName ?? defaultSecondAreaSupport.areaName);
}

export function getSecondAreaDefaultName() {
  const areaConfig = getAreaConfig();
  return i18n.t(areaConfig?.secondArea?.defaultName ?? defaultSecondAreaSupport.defaultName);
}

export interface IAppBaseInfo {
  appId: string;
  appSecret: string;
}

export interface IAppInfo extends IAppBaseInfo {
  platform: string;
}


export function getAllAppIDInfos() {
  const gconfig = getTemplateConfig();
  const infoList: IAppInfo[] = [];
  let appId = gconfig.gizwits.androidAppID;
  let appSecret = gconfig.gizwits.androidAppSecret;
  if (appId && appSecret) {
    infoList.push({ appId, appSecret, platform: 'android' });
  }

  appId = gconfig.gizwits.iosAppID;
  appSecret = gconfig.gizwits.iosAppSecret;
  if (appId && appSecret) {
    infoList.push({ appId, appSecret, platform: 'ios' });
  }

  appId = gconfig.gizwits.wxAppID;
  appSecret = gconfig.gizwits.wxAppSecret;
  if (appId && appSecret) {
    infoList.push({ appId, appSecret, platform: 'wx' });
  }

  return infoList;
}

export default config;
